package com.lzq.rabbitmqdemo.advanced_features.constant;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbiMQConfig {

    /**
     * 接收者确认
     *
     */
    @Bean("ackExchange")
    public Exchange ackExchange() {
        return ExchangeBuilder.topicExchange(Constants.ACK_EXCHANGE_NAME).durable(true).build();
    }

    @Bean("ackQueue")
    public Queue ackQueue() {
        return QueueBuilder.durable(Constants.ACK_QUEUE).build();
    }

    @Bean("ackBinding")
    public Binding ackBinding(@Qualifier("ackExchange") Exchange exchange, @Qualifier("ackQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("ack").noargs();
    }

    /**
     * 发布者确认
     *
     */

    @Bean("confirmExchange")
    public Exchange confirmExchange() {
        return ExchangeBuilder.topicExchange(Constants.CONFIRM_EXCHANGE_NAME).durable(true).build();
    }

    @Bean("confirmQueue")
    public Queue confirmQueue() {
        return QueueBuilder.durable(Constants.CONFIRM_QUEUE).build();
    }

    @Bean("confirmBinding")
    public Binding confirmBinding(@Qualifier("confirmExchange") Exchange exchange, @Qualifier("confirmQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("confirm").noargs();
    }

    /**
     * 重试机制
     *
     */
    @Bean("retryExchange")
    public FanoutExchange retryExchange() {
        return ExchangeBuilder.fanoutExchange(Constants.RETRY_EXCHANGE_NAME).durable(true).build();
    }

    @Bean("retryQueue")
    public Queue retryQueue() {
        return QueueBuilder.durable(Constants.RETRY_QUEUE).build();
    }

    @Bean("retryBinding")
    public Binding retryBinding(@Qualifier("retryExchange") FanoutExchange exchange, @Qualifier("retryQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange);
    }

    /**
     * ttl
     *
     */
    @Bean("ttlExchange")
    public FanoutExchange ttlExchange() {
        return ExchangeBuilder.fanoutExchange(Constants.TTL_EXCHANGE_NAME).durable(true).build();
    }

    @Bean("ttlQueue")
    public Queue ttlQueue() {
        return QueueBuilder.durable(Constants.TTL_QUEUE).build();
    }

    @Bean("ttlBinding")
    public Binding ttlBinding(@Qualifier("ttlExchange") FanoutExchange exchange, @Qualifier("ttlQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange);
    }

    //设置队列的属性
    /*@Bean("ttlQueue2")
    public Queue ttlQueue2() {
        return QueueBuilder.durable(Constants.TTL_QUEUE).ttl(20*1000).build();
    }*/

    //设置消息的ttl
    @Bean("ttlQueue2")
    public Queue ttlQueue2() {
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-message-ttl",20000);//20秒过期
        return QueueBuilder.durable(Constants.TTL_QUEUE2).withArguments(arguments).build();
    }

    @Bean("ttlBinding2")
    public Binding ttlBinding2(@Qualifier("ttlExchange") FanoutExchange exchange, @Qualifier("ttlQueue2") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange);
    }

    /**
     * 死信队列
     *
     */
    @Bean("normalExchange")
    public Exchange normalExchange() {
        return ExchangeBuilder.topicExchange(Constants.NORMAL_EXCHANGE_NAME).durable(true).build();
    }

    //正常队列绑定死信交换机
    @Bean("normalQueue")
    public Queue normalQueue() {
        return  QueueBuilder.durable(Constants.NORMAL_QUEUE)
                .deadLetterExchange(Constants.DLX_EXCHANGE_NAME)
                .deadLetterRoutingKey("dlx")
//                .ttl(10*1000)
//                .maxLength(10L)
                .build();
    }

    @Bean("normalBinding")
    public Binding normalBinding(@Qualifier("normalExchange") Exchange exchange, @Qualifier("normalQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("normal").noargs();
    }

    @Bean("dlxExchange")
    public Exchange dlxExchange() {
        return ExchangeBuilder.topicExchange(Constants.DLX_EXCHANGE_NAME).durable(true).build();
    }

    @Bean("dlxQueue")
    public Queue dlxQueue() {
        return QueueBuilder.durable(Constants.DLX_QUEUE).build();
    }

    @Bean("dlxBinding")
    public Binding dlxBinding(@Qualifier("dlxExchange") Exchange exchange, @Qualifier("dlxQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("dlx").noargs();
    }


}
